
package kaizone.haining.chaobohui.json;

import java.io.Serializable;
import java.util.ArrayList;

import kaizone.haining.chaobohui.json.Item.PicModel;

import org.json.JSONArray;
import org.json.JSONObject;

public class EntityHuodong extends JSONResponse implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static final String KEY = "huodong";

    public Data getData() {
        return data;
    }

    public void setData(Data data) {
        this.data = data;
    }

    private Data data;

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append(super.toString()).append("\n");
        sb.append(data.toString()).append("\n");
        return sb.toString();
    }

    public static EntityHuodong parseJson(String str) {
        EntityHuodong entiryHuodong = new EntityHuodong();
        try {
            JSONObject jsonObject = new JSONObject(str);

            JSONResponse response = JSONResponse.parse(jsonObject);
            entiryHuodong.setJSONResponse(response);

            JSONObject jsondata = jsonObject.optJSONObject(JSONResponse.DATA);
            if (jsondata != null) {
                Data data = new Data();
                //
                JSONObject jsonPagination = jsondata.optJSONObject(Data.PAGINATION);
                if (jsonPagination != null) {
                    Pagination pagination = Pagination.parse(jsonPagination);
                    data.setPagination(pagination);
                }
                //
                String jsonTotalRecords = jsondata.optString(Data.TOTALRECORDS);
                if (jsonTotalRecords != null) {
                    data.setTotalRecords(jsonTotalRecords);
                }
                //
                JSONArray jsonitems = jsondata.optJSONArray(Data.ITEMS);
                if (jsonitems != null) {
                    ArrayList<Item> items = new ArrayList<Item>();
                    int len = jsonitems.length();
                    for (int i = 0; i < len; i++) {
                        JSONObject jsonitem = jsonitems.optJSONObject(i);
                        Item item = Item.parse(jsonitem);
                        items.add(item);
                    }
                    data.setItems(items);
                }
                entiryHuodong.setData(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return entiryHuodong;
    }

}
